package com.yan.config;


import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.id.generator.self.CommonSelfIdGenerator;
import com.dangdang.ddframe.rdb.sharding.id.generator.self.time.AbstractClock;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

/**
 * https://www.jianshu.com/p/74c02a2a89de    简书
 *
 * https://blog.csdn.net/zjt1388/article/details/52411901
 *
 *
 * https://segmentfault.com/a/1190000014037539   分布式ID
 * Created by yanl_ on 2018/5/6.
 */
@Configuration
@MapperScan(basePackages = "com.yan.mapper", sqlSessionTemplateRef  = "sqlSessionTemplate")
public class DataSourceConfig {

    /**
     * 配置数据源0，数据源的名称最好要有一定的规则，方便配置分库的计算规则
     * @return
     */

    @Bean(name = "dataSource0")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    public DataSource dataSource0() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 配置数据源1，数据源的名称最好要有一定的规则，方便配置分库的计算规则
     * @return
     */
    @Bean(name="dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource dataSource1(){
        return DataSourceBuilder.create().build();
    }

    /** * 配置数据源规则，即将多个数据源交给sharding-jdbc管理，并且可以设置默认的数据源，
     *  * 当表没有配置分库规则时会使用默认的数据源 *
     *  @param dataSource0
     *  * @param dataSource1
     *  *@return
     *  */
    @Bean
    public DataSourceRule dataSourceRule(@Qualifier("dataSource0") DataSource dataSource0, @Qualifier("dataSource1") DataSource dataSource1) {
        //设置分库映射  设置默认库，两个库以上时必须设置默认库。默认库的数据源名称必须是dataSourceMap的key之一
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        dataSourceMap.put("dataSource0", dataSource0);
        dataSourceMap.put("dataSource1", dataSource1);
        return new DataSourceRule(dataSourceMap, "dataSource0");

    }

    /**
     * 绑定表策略，
     * 在查询时会使用主表策略计算路由的数据源，
     * 因此需要约定绑定表策略的表的规则需要一致，可以一定程度提高效率
     * @param dataSourceRule
     * @return
     */
    @Bean
    public ShardingRule shardingRule(DataSourceRule dataSourceRule){
    //具体分库分表策略
    TableRule orderTableRule = TableRule.builder("t_order").
            actualTables(Arrays.asList("t_order_0", "t_order_1")).
            tableShardingStrategy(new TableShardingStrategy("orderId", new TableShardingAlgorithm())).
            dataSourceRule(dataSourceRule).build();

        List<BindingTableRule> bindingTableRules = new ArrayList<BindingTableRule>();
        bindingTableRules.add(new BindingTableRule(Arrays.asList(orderTableRule)));
        return ShardingRule.builder().
                dataSourceRule(dataSourceRule).
                tableRules(Arrays.asList(orderTableRule)).
                bindingTableRules(bindingTableRules).
                databaseShardingStrategy(new DatabaseShardingStrategy("userId", new DatabaseShardingAlgorithm())).
                tableShardingStrategy(new TableShardingStrategy("orderId", new TableShardingAlgorithm())).
                build();
    }



    /**
     * 创建sharding-jdbc的数据源DataSource，MybatisAutoConfiguration会使用此数据源
     * @param shardingRule * @return * @throws SQLException
     */
    @Bean(name="dataSource")
    public DataSource shardingDataSource(ShardingRule shardingRule) throws SQLException {
        return ShardingDataSourceFactory.createDataSource(shardingRule);
    }


    /**
     * 需要手动配置事务管理器
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactitonManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }






    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/**/*Mapper.xml"));
        return bean.getObject();
    }


    @Bean(name = "sqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }





//    @Bean
//    public CommonSelfIdGenerator commonSelfIdGenerator() {
//        CommonSelfIdGenerator.setClock(AbstractClock.systemClock());
//        CommonSelfIdGenerator commonSelfIdGenerator = new CommonSelfIdGenerator();
//   //     commonSelfIdGenerator.setWorkerId(1L);
//        return commonSelfIdGenerator;
//    }



    @Bean
    public IdGenerator getIdGenerator() {
        CommonSelfIdGenerator.setClock(AbstractClock.systemClock());
        CommonSelfIdGenerator commonSelfIdGenerator = new CommonSelfIdGenerator();
        //     commonSelfIdGenerator.setWorkerId(1L);
        return commonSelfIdGenerator;

    }
}